from rest_framework import serializers
from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet

from apps.goods.models import GoodsChannel, GoodsChannelGroup
from apps.meiduo_admin.utils import CurPagination


class GoodsChannelSerializer(serializers.ModelSerializer):
    group = serializers.StringRelatedField(read_only=True)
    group_id = serializers.IntegerField(required=True, min_value=0)
    category = serializers.StringRelatedField(read_only=True)
    category_id = serializers.IntegerField(required=True, min_value=0)

    class Meta:
        model = GoodsChannel
        exclude = ['create_time', 'update_time']


class GoodsChannelView(ModelViewSet):
    permission_classes = [IsAdminUser]
    queryset = GoodsChannel.objects.all().order_by('id')
    serializer_class = GoodsChannelSerializer
    pagination_class = CurPagination

    def channel_group(self, request):
        """频道分组信息"""
        data = GoodsChannelGroup.objects.order_by('id').values('id', 'name')
        return Response(data)
